import React, { Component } from "react";
import moment from 'moment';

export default class Clock extends Component {

  timer = null;

  state = {
    timeStr: moment().format('YYYY-MM-DD HH:mm:ss')
  }

  render() {
    return <div style={{ border: "solid 1px #666", padding: '20px 40px', display: 'inline-block' }}>
      {this.state.timeStr}
    </div>;
  }

  componentDidMount(){
    //启动定时器
    this.timer = setInterval(() => {
      console.log(123);
      // 更新状态
      this.setState({
        timeStr: moment().format('YYYY-MM-DD HH:mm:ss')
      })
    }, 1000);
  }

  componentWillUnmount(){
    //停止定时器
    clearInterval(this.timer);
  }
  
}
